package com.it2297.view;

import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.it2297.component.AbstractPagination;
import com.it2297.controller.ProductController;
import com.it2297.main.UserApp;
import com.it2297.model.OrderItem;
import com.it2297.model.Payment;
import com.it2297.model.Product;

import javax.swing.SpinnerNumberModel;
import javax.swing.border.BevelBorder;
import javax.swing.SwingConstants;

public class ShoppingCartView extends JPanel {
	private ProductController pc = new ProductController();
	private static JScrollPane scrollpane = new JScrollPane();
	private ArrayList<Product> tempList = new ArrayList<Product>();
	private final JToolBar toolBar = new JToolBar();
	private final JButton btn_submit = new JButton("Check out");
	private int orderID;
	private int id;
	private String title = "ProductName";
	private String price = "Price";
	private String msg = title + padLeft(" ", (20 - title.length())) + price +  "\n";
	public ShoppingCartView() {
		pc = new ProductController(this);
		initGUI();
	}

	public void initGUI() {
		this.setBounds(100, 100, 750, 570);
		this.setLayout(new BorderLayout(0, 0));
		this.add(getScrollpane());

		toolBar.setFloatable(false);
		btn_submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!pc.getOrderList().isEmpty()) {
					int n = JOptionPane.showConfirmDialog(null,
							"Please Conifrm Your Order.", "Confirm Order",
							JOptionPane.YES_NO_OPTION);
					if (n == JOptionPane.YES_OPTION) {
						for(int a=0; a<pc.getOrderList().size();a++){
							int temp_quantity = pc.getQuantity(pc.getOrderList().get(a).getProductid());
							if(temp_quantity > pc.getQuantity().get(a)){
								int new_quantity = temp_quantity - pc.getQuantity().get(a);
								pc.updatedQuantityCart(new_quantity, pc.getOrderList().get(a).getProductid());
								orderID = pc.CreatePayment(pc.getTotalPrice());
								id = pc.CreateOrderList(orderID, UserApp.getCurrentUser().getNric());
								
								for(int i=0; i< pc.getOrderList().size();i++){
									pc.CreateOrderItem(pc.getQuantity().get(i), pc.getOrderList().get(i).getProductid(), id);
								}
								
								
								
								JOptionPane.showMessageDialog(null,"Thank you for shopping with us! Hope to see you again!");
								OrderHistory.getScrollpane().getViewport().removeAll();
								pc.clear();
								pc.setOrderlistHistory(UserApp.getCurrentUser().getNric());
								pc.addOrderHistory();
								
								pc.addProducts();
								
								
							}else{
								JOptionPane.showMessageDialog(null, "Not enough stock");
								break;
							}
						}
						ArrayList<OrderItem> orders = pc.getShoppingList(id);
						for(int i =0; i<orders.size();i++ ){
							tempList.add(pc.getProduct(orders.get(i).getProdID()));
						}
						System.out.println(tempList.size());
						for(int ii=0; ii< tempList.size();ii++){
							msg = msg + tempList.get(ii).getProdName() + padLeft(" ", (20 - tempList.get(ii).getProdName().length())) + Double.toString(tempList.get(ii).getPrice() * orders.get(ii).getQuantity()) + "\n";
						}
						boolean success = pc.sendEmailShopping(id, msg);
						if(success){
							JOptionPane.showMessageDialog(null, "Your Order list has been send to your email");
							tempList.clear();
						}
						
					} else {
						JOptionPane.showMessageDialog(null,
								"Please contiune your shopping!");
					}
				}else{
					JOptionPane.showMessageDialog(null,
							"You order list is empty!");
				}
			}
		});
		toolBar.add(btn_submit);
		add(toolBar, BorderLayout.SOUTH);

		btn_submit.setIcon(new ImageIcon(NewShopView.class
				.getResource("/com/alee/extended/filechooser/icons/edit.png")));
		btn_submit.setFocusable(false);

		toolBar.add(btn_submit);

		pc.addProducts();
	}

	public static void setPanel(JPanel panel) {
		getScrollpane().setViewportView(panel);
	}

	public static JScrollPane getScrollpane() {
		return scrollpane;
	}
	public String padRight(String s, int n) {
		return String.format("%1$-" + n + "s", s);
	}

	public String padLeft(String s, int n) {
		return String.format("%1$" + n + "s", s);
	}

}
